package com.itfreer.ui.base;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.View;

import com.itfreer.ui.R;
import com.itfreer.utils.StringUtils;
import com.itfreer.ui.standard.IPower;

/**
 * 自定义TextView，解决字体符号绑定问题
 */
public class TextView extends android.support.v7.widget.AppCompatTextView implements IPower {

    private String fontName;
    private String securityID;
    private boolean securityVisible=true;

    public TextView(Context context) {
        super(context);
        setCustomTypeface();
    }

    public TextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        getFontName(context, attrs);
        setCustomTypeface();
    }

    public TextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        getFontName(context, attrs);
        setCustomTypeface();
    }

    protected void getFontName(Context context, AttributeSet attrs){
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SimpleView);
        fontName = a.getString(R.styleable.SimpleView_fontName);
        securityID = a.getString(R.styleable.SimpleView_securityID);
    }

    protected void setCustomTypeface(){
        if(StringUtils.IsNullOrEmpty(fontName)){
            fontName = "ui.ttf";
        }
        Typeface font = Typeface.createFromAsset(this.getContext().getAssets(), fontName);
        setTypeface(font);

    }

    /**
     * 获取权限ID
     * @return
     */
    public String getSecurityID(){
        return securityID;
    }

    /**
     * 没有权限时，设置是否可见
     * @param visible
     */
    public void setSecurityVisible(boolean visible){
        securityVisible = visible;
        if(visible){
            this.setVisibility(View.VISIBLE);
        }else{
            // 表示不可见并且不占用空间
            this.setVisibility(View.GONE);
        }
    }
}